package com.qf.day14;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.stream.Stream;

/**
 把文本文件中的IP地址进行去重，然后打印出来* 1.
 统计一个文本文件中IP地址的出现的次数(IP地址格式为192.168.10.39，每行只存放一个IP地址)’ 2.
 */
public class HashmapDemo {

    public static void main(String[] args) throws FileNotFoundException {
        File file=new File("F:\\2345Downloads\\vxjilu\\WeChat Files\\wxid_8c1rlr7kkau922\\FileStorage\\File\\2024-03\\IP.txt");
        Scanner scanner = new Scanner(file);
        HashMap<String,Integer>hashMap=new HashMap<>();
        while (scanner.hasNext()){
            String line=scanner.nextLine();
            if(hashMap.containsKey(line)){
                hashMap.put(line, hashMap.get(line)+1);
            }else{
                hashMap.put(line,1);
            }
        }
        Set<String> strings = hashMap.keySet();
        System.out.println(strings);
        System.out.println(hashMap);
        Collection<Integer> values = hashMap.values();
        Integer max = Collections.max(values, new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o1 - o2;
            }
        });
        System.out.println(max);

        ArrayList<String>arrayList=new ArrayList<>();
        //遍历
        for (Map.Entry<String, Integer> stringIntegerEntry : hashMap.entrySet()) {
            if(stringIntegerEntry.getValue().equals(max)){
                String key = stringIntegerEntry.getKey();
                arrayList.add(key);
            }

        }
        System.out.println(arrayList);
        //排序
        Stream<Map.Entry<String, Integer>> sorted = hashMap.entrySet().stream().sorted(new Comparator<Map.Entry<String, Integer>>() {
            @Override
            public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
                return o2.getValue() - o1.getValue();
            }
        });
        
        //遍历

        sorted.forEach(new Consumer<Map.Entry<String, Integer>>() {
            @Override
            public void accept(Map.Entry<String, Integer> stringIntegerEntry) {

                System.out.println(stringIntegerEntry);

            }
        });


//        //lambda遍历
//        System.out.println("foreach,lambda遍历");
//        hashMap.forEach(new BiConsumer<String, Integer>() {
//            @Override
//            public void accept(String s, Integer integer) {
//                System.out.println(s+"="+integer);
//            }
//        });
//        System.out.println("lambda遍历");
//        hashMap.forEach((s,integer)->System.out.println(s+"="+integer));
//        //遍历3
//        System.out.println("增强for遍历");
//        Set<String> keys = hashMap.keySet();
//        for (String key : keys) {
//            Integer value=hashMap.get(key);
//            System.out.println(key+"="+value);
//        }

        HashMap<Integer,String>h1=new HashMap<>();
        h1.put(1,"a");
        h1.put(17,"c");
        h1.put(2,"b");
        h1.put(18,"c");

        System.out.println(h1);
    }
}
